package com.szu.subject.domains;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 课程信息
 * 
 * @author ShenXiaolin
 *
 */
public class Course extends BaseModel implements Comparable<Course>{
	private String account;
	private String name;
	private List<ClassRoomInfo> classRoomInfos;// 上课教室及时间
	private Double credit;// 学分
	private Short examine;// 0:期末考试为主
	private String weeks;
	private String remarks;// 备注
	private Short creditType;// 0:理科，1：文科，2：体育课
	private Long collegeId;// 学院id
	private int primaryUpperlimit=0;// 主选上限
	private int secondaryUpperlimit=0;// 非主选上限
	private String classIds;// 主选班级id,为空的时候主选上限为空
	private String teacherName;
	private String description;
	private boolean primaryAvailable=true;// 主选上限是否达到
	private boolean secondaryAvailable=true;// 非主选上限是否达到
	private String addresses;
	private String times;
	private List<TimeObject> timeObjects;
	
	private boolean courseType;//0：必修，1：选修，前端传过来
	
	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<ClassRoomInfo> getClassRoomInfos() {
		return classRoomInfos;
	}

	@JsonIgnore
	public List<TimeObject> getTimeObjects() {
		return timeObjects;
	}

	public void setTimeObjects() {
		this.timeObjects = TimeObject.geTimeObject(times);
	}

	public void setClassRoomInfos(List<ClassRoomInfo> classRoomInfos) {
		this.classRoomInfos = classRoomInfos;
	}
	
	public void setClassRoomInfos() {
		List<ClassRoomInfo> roomInfos=new ArrayList<>();
		String[] addressList=addresses.split(",");
		String[] timeList=times.split(",");
		int length=addressList.length;
		for(int i=0;i<length;i++){
			ClassRoomInfo item=new ClassRoomInfo(addressList[i],timeList[i]);
			roomInfos.add(item);
		}
		setClassRoomInfos(roomInfos);
	}
	
	public Double getCredit() {
		return credit;
	}

	public void setCredit(Double credit) {
		this.credit = credit;
	}

	public Short getExamine() {
		return examine;
	}

	public void setExamine(Short examine) {
		this.examine = examine;
	}

	public String getWeeks() {
		return weeks;
	}

	public void setWeeks(String weeks) {
		this.weeks = weeks;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public Short getCreditType() {
		return creditType;
	}

	public void setCreditType(Short creditType) {
		this.creditType = creditType;
	}

	public Long getCollegeId() {
		return collegeId;
	}

	public void setCollegeId(Long collegeId) {
		this.collegeId = collegeId;
	}

	public String getClassIds() {
		return classIds;
	}

	public void setClassIds(String classIds) {
		this.classIds = classIds;
	}

	public String getAddresses() {
		return addresses;
	}

	public void setAddresses(String addresses) {
		this.addresses = addresses;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public boolean isPrimaryAvailable() {
		return primaryAvailable;
	}

	public void setPrimaryAvailable(boolean primaryAvailable) {
		this.primaryAvailable = primaryAvailable;
	}

	public boolean isSecondaryAvailable() {
		return secondaryAvailable;
	}

	public void setSecondaryAvailable(boolean secondaryAvailable) {
		this.secondaryAvailable = secondaryAvailable;
	}
	
	public String getAddress() {
		return addresses;
	}

	public void setAddress(String address) {
		this.addresses = address;
	}

	public String getTimes() {
		return times;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void setTimes(String times) {
		this.times = times;
		setTimeObjects();
	}

	
	public boolean isTimeAvailable(List<Course> courses){
		return isTimeAvailable(courses,0);
	}
	
	public boolean isTimeAvailable(List<Course> courses,int index){
		if(StringUtils.isEmpty(times)){
			return true;
		}
		List<TimeObject> timeThis=this.getTimeObjects();
		int length=courses.size();
		for(int i=index;i<length;i++){
			Course course=courses.get(i);
			if(StringUtils.isEmpty(course.getTimes())){
				return true;
			}
			List<TimeObject> timeHaven=course.getTimeObjects();;
			for(int j=0;j<timeThis.size();j++){
				if(timeHaven.contains(timeThis.get(j)))
					return false;
			}
		}
		return true;
	}
	
	/**
	 * 重写equal函数，学生不能选择两门一样(名字相同)的课程
	 * @param course
	 * @return
	 */
	@Override
	public boolean equals(Object o) {
		Course course=(Course) o;
		if (name.equals(course.getName())) {
			return true;
		} else if(course.getAccount().equals(getAccount())||course.getId().equals(getId())){
			return true;
		}
		return false;
	}

	@Override
	public int hashCode() {
		return name.hashCode();
	}
	
	
	
/*	public Long getAccountLong(){
		return Long.valueOf(account);
	}*/

	public int getPrimaryUpperlimit() {
		return primaryUpperlimit;
	}

	public void setPrimaryUpperlimit(int primaryUpperlimit) {
		this.primaryUpperlimit = primaryUpperlimit;
		if(primaryUpperlimit==0){
			setPrimaryAvailable(false);
		}
		else setPrimaryAvailable(true);
	}

	public int getSecondaryUpperlimit() {
		return secondaryUpperlimit;
	}

	public void setSecondaryUpperlimit(int secondaryUpperlimit) {
		this.secondaryUpperlimit = secondaryUpperlimit;
		if(secondaryUpperlimit==0){
			setSecondaryAvailable(false);
		}
		else setSecondaryAvailable(true);
	}

	public boolean getCourseType() {
		return courseType;
	}

	public void setCourseType(boolean courseType) {
		this.courseType = courseType;
	}

	@Override
	public int compareTo(Course o) {
		if(this.getId()>o.getId())
			return 1;
		else if (this.getId()<o.getId()) {
			return -1;
		}
		return 0;
	}
	
}
